import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/home/Home.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: ' Register',
        component: () => import('../views/Regist.vue')
    },
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/search',
        name: 'Search',
        component: () => import('../views/searchService/index.vue')
    },
    // 商品详情页
    {
        path: '/detail/:id',
        name: 'Detail',
        component: () => import('../views/detail/ItemDetail.vue')
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import('../views/order/Order.vue')
    },
    {
        path: '/cart',
        name: 'Cart',
        component: () => import('../views/cart/Cart.vue')
    },
    {
        path: '/user_detail',
        name: 'UserDetail',
        component: () => import('../views/userDetail/index.vue')
    },
    // 店铺
    {
        path: '/shop_store',
        name: 'ShopStore',
        component: () => import('../views/shop/index.vue'),
        redirect: '/shop_store/shop_list',
        children: [
            {
                path: 'shop_list',
                name: 'ShopList',
                component: () => import('../views/shop/components/MyShop.vue')
            },
            {
                path: 'publish_service',
                name: 'ServiceList',
                component: () => import('../views/shop/components/MyService.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

const token = localStorage.getItem('vehicle_web_token')

router.beforeEach((to, from, next) => {
    if (!token && !['/', '/home', '/login', '/register'].includes(to.path)) {
        if (to.path.includes('/detail')) {
            next()
        }

        next('/login')
    }
    next()
})

export default router
